Ext.define('BISC.view.CtxMnuMonitorList', {
    extend: 'Ext.menu.Menu',
    scope: this,
    listeners: {
        click: function( menu, item ) {
            switch( item.identifier ) {
                case 'uploadall':
                    this.uploadall();
                    break;
                case 'pullall':
                    this.pullall();
                    break;
                case 'stopmonitor':
                    this.stopmonitor();
                    break;
            }
        }
    },
    initComponent: function() {
        var me = this;

        Ext.applyIf(me, {
            items: [
                {
                    text: 'Upload all to server',
                    iconCls: 'icon_upload',
                    identifier: 'uploadall'
                },
                {
                    text: 'Pull all from server',
                    iconCls: 'icon_pull',
                    identifier: 'pullall'
                },
                '-',
                {
                    text: 'Stop Monitoring',
                    iconCls: 'icon_watch',
                    identifier: 'stopmonitor'
                }
            ]
        });
        me.callParent(arguments);
    },
    uploadall: function() {
        var obj = {type: 'folders', ids: [ this.record.data.id ]};
        Ext.data.JsonP.request({url: 'http://localhost:8888/images/add',
            timeout: 120000,
            scope: this,
            params: {
                'types[]': Ext.encode( obj ),
                bisId: 1 
            },
            success: function(responseObject) {
                if(responseObject.success) {
                    console.log('Uploaded');
                    Ext.StoreManager.lookup('ImagesStore').load();
                } else {
                    console.log('Not Uploaded');
                }
            },
            failure: function() {
                console.log('In Ajax failure function');
            }
        });
    },
    pullall: function() {
        var obj = {type: 'folders', ids: [ this.record.data.id ]};
        Ext.data.JsonP.request({url: 'http://localhost:8888/images/delete',
            timeout: 120000,
            scope: this,
            params: {
                'types[]': Ext.encode( obj ),
                bisId: 1 
            },
            success: function( responseObject ) {
                if ( responseObject.success ) {
                    console.log('Pulled');
                    Ext.StoreManager.lookup('ImagesStore').load();
                } else {
                    console.log('Not pulled');
                }
            },
            failure: function() {
                console.log('In Ajax failure function');
            }
        });
    },
    stopmonitor: function() {
        var me = this;
        Ext.Msg.show({
            title: 'Pull Images',
            msg: 'Do you also want to pull any uploaded images from the BIS server?',
            buttons: Ext.Msg.YESNOCANCEL,
            icon: Ext.Msg.QUESTION,
            fn: function( btn, nothing, item ) {
                if ( btn != 'cancel' ) {
                    Ext.data.JsonP.request({url: 'http://localhost:8888/monitors/remove/' + me.record.data.id,
                        timeout: 120000,
                        scope: this,
                        params: {
                            pull: btn == 'yes'
                        },
                        success: function( responseObject ) {
                            if ( responseObject.success ) {
                                Ext.StoreManager.lookup('FolderListStore').load();
                                Ext.StoreManager.lookup('ImagesStore').load();
                            }
                        },
                        failure: function() {
                            console.log('In Ajax failure function');
                        }
                    });
                }
            }
        });
    }
});
